package org.azmat.demojpa.Controller.JDBC;

import org.azmat.demojpa.Messages.Messages;
import org.azmat.demojpa.SQL.JDBC.Entity.Student;
import org.azmat.demojpa.SQL.JDBC.Repository.StudentRepository;
import org.azmat.demojpa.SQL.JDBC.Service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

@Controller
public class StudentController {

    @Autowired
    private StudentService studentService;

    // 创建并返回ModelAndView对象
    private ModelAndView createModelAndView(String viewName) {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName(viewName);
        return modelAndView;
    }

    // 创建并返回ModelAndView对象，同时添加一个属性
    private ModelAndView createModelAndView(String viewName, String attributeName, Object attributeValue) {
        ModelAndView modelAndView = createModelAndView(viewName);
        modelAndView.addObject(attributeName, attributeValue);
        return modelAndView;
    }

    @GetMapping("/student")
    public ModelAndView findAllStudent() {
        List<Student> studentList = studentService.findAll();
        return createModelAndView("student", "studentList", studentList);
    }

    @PostMapping("/student/add")
    public ModelAndView addStudent(Student student, RedirectAttributes redirectAttributes) {
        studentService.addStudent(student);
        redirectAttributes.addFlashAttribute("message", Messages.STUDENT_ADDED);
        return createModelAndView("redirect:/student");
    }

    @GetMapping("/student/{id}")
    public ModelAndView selectStudentById(@PathVariable("id") int id, RedirectAttributes redirectAttributes) {
        Student student = studentService.getStudentById(id);
        if (student == null) {
            redirectAttributes.addFlashAttribute("message", Messages.STUDENT_NOT_FOUND);
            return createModelAndView("redirect:/student");
        }
        return createModelAndView("student", "student", student);
    }

    @GetMapping("/student/delete/{id}")
    public ModelAndView deleteStudent(@PathVariable("id") int id, RedirectAttributes redirectAttributes) {
        studentService.deleteStudent(id);
        redirectAttributes.addFlashAttribute("message", Messages.STUDENT_DELETED);
        return createModelAndView("redirect:/student");
    }

    @GetMapping("/student/edit/{id}")
    public ModelAndView editStudent(@PathVariable("id") int id, RedirectAttributes redirectAttributes) {
        Student student = studentService.getStudentById(id);
        if (student == null) {
            redirectAttributes.addFlashAttribute("message", Messages.STUDENT_NOT_FOUND);
            return createModelAndView("redirect:/student");
        }
        return createModelAndView("edit", "student", student);
    }

    @PostMapping("/student/update")
    public ModelAndView updateStudent(Student student, RedirectAttributes redirectAttributes) {
        studentService.updateStudent(student);
        redirectAttributes.addFlashAttribute("message", Messages.STUDENT_UPDATED);
        return createModelAndView("redirect:/student");
    }

    // get请求返回add页面
    @GetMapping("/student/add")
    public ModelAndView addStudent() {
        return createModelAndView("add");
    }

    @Autowired
    private StudentRepository studentRepository;

    @GetMapping("/student/page")
    public ModelAndView findAll(@RequestParam(value = "start", defaultValue = "0") Integer start,
                                @RequestParam(value = "size", defaultValue = "10") Integer size) {
        ModelAndView modelAndView = new ModelAndView();
        // 构造Pageable，默认查询第1页，每页显示3条记录，根据属性id降序排序
        Pageable pageable = PageRequest.of(start, size, Sort.by("id").descending());
        Page<Student> studentList = studentRepository.findAll(pageable);
        modelAndView.addObject("studentList", studentList);
        modelAndView.setViewName("studentPage");
        return modelAndView;
    }
}
